package com.mvp.shq.washer.ui.modifyMobile;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.StringUtils;

/**
 * Created by Administrator on 2017/8/31.
 */

public class Presenter extends ModifyMobileConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void code(String mobile) {
        String token = SpUtil.getToken();
        model.code(token,mobile)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.codeF(t.getMsg());
                        }else {
                            view.codeS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.codeF("获取失败");
                    }
                });
    }

    @Override
    void modify(String oldPwd, String newMobile, String code) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        oldPwd = StringUtils.MD5(oldPwd);
        model.modify(token,userId,oldPwd,newMobile,code)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.modifyF(t.getMsg());
                        }else {
                            view.modifyS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.modifyF("修改失败");
                    }
                });
    }
}
